<template>
  <div class="channel-info-container">
    <div class="page-header">
      <h1>支付渠道信息</h1>
      <p>当前系统中已注册的支付渠道和实现详情</p>
    </div>

    <!-- 统计信息 -->
    <div class="stats-section">
      <div class="stat-card">
        <div class="stat-number">{{ channelCount }}</div>
        <div class="stat-label">支付渠道</div>
      </div>
      <div class="stat-card">
        <div class="stat-number">{{ implementationCount }}</div>
        <div class="stat-label">渠道实现</div>
      </div>
      <div class="stat-card">
        <div class="stat-number">{{ availableCount }}</div>
        <div class="stat-label">可用实现</div>
      </div>
    </div>

    <!-- 渠道实现列表 -->
    <div class="implementations-section">
      <h2>渠道实现列表</h2>
      <div class="implementations-grid">
        <div
          v-for="impl in implementations"
          :key="impl.implementationKey"
          class="implementation-card"
          :class="{ 'unavailable': !impl.available }"
        >
          <div class="card-header">
            <div class="implementation-title">
              <h3>{{ impl.implementationKey }}</h3>
              <div class="badges">
                <span class="badge status" :class="{ 'available': impl.available, 'unavailable': !impl.available }">
                  {{ impl.available ? '可用' : '不可用' }}
                </span>
                <span class="badge default" v-if="impl.default">默认</span>
              </div>
            </div>
          </div>

          <div class="card-body">
            <div class="info-row">
              <span class="label">渠道名称:</span>
              <span class="value">{{ impl.channelName }}</span>
            </div>
            <div class="info-row">
              <span class="label">渠道ID:</span>
              <span class="value">{{ impl.channelId }}</span>
            </div>
            <div class="info-row">
              <span class="label">实现类型:</span>
              <span class="value">{{ impl.implementationType }}</span>
            </div>
            <div class="info-row">
              <span class="label">支付产品:</span>
              <div class="pay-products">
                <span
                  v-for="product in impl.supportedPayProducts"
                  :key="product"
                  class="product-tag"
                >
                  {{ product }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>正在加载渠道信息...</p>
    </div>

    <!-- 错误提示 -->
    <div v-if="error" class="error-container">
      <div class="error-icon">❌</div>
      <h3>加载失败</h3>
      <p>{{ error }}</p>
      <button @click="loadChannelInfo" class="retry-button">重试</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { PaymentApi, type ChannelImplementationInfo } from '@/api/payment'
import { AnyNotification } from '@/components/AnyNotification'

// 响应式数据
const loading = ref(true)
const error = ref('')
const implementations = ref<ChannelImplementationInfo[]>([])

// 计算属性
const channelCount = computed(() => {
  const channelIds = new Set(implementations.value.map(impl => impl.channelId))
  return channelIds.size
})

const implementationCount = computed(() => implementations.value.length)

const availableCount = computed(() => {
  return implementations.value.filter(impl => impl.available).length
})

// 加载渠道信息
const loadChannelInfo = async () => {
  loading.value = true
  error.value = ''

  try {
    const data = await PaymentApi.getAllChannelImplementationInfo()
    implementations.value = data

    if (data.length === 0) {
      AnyNotification.generalError('当前系统中没有注册的支付渠道')
    }
  } catch (err) {
    error.value = err instanceof Error ? err.message : '加载渠道信息失败'
    AnyNotification.generalError(error.value)
  } finally {
    loading.value = false
  }
}

// 页面加载时获取数据
onMounted(() => {
  loadChannelInfo()
})
</script>

<style scoped>
.channel-info-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 32px;
}

.page-header h1 {
  font-size: 28px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 8px 0;
}

.page-header p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

/* 统计信息 */
.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
  border: 1px solid #e8e8e8;
}

.stat-number {
  font-size: 36px;
  font-weight: 700;
  color: #1890ff;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

/* 渠道实现列表 */
.implementations-section h2 {
  font-size: 22px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 24px;
}

.implementations-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
}

.implementation-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e8e8e8;
  overflow: hidden;
  transition: all 0.3s ease;
}

.implementation-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.implementation-card.unavailable {
  opacity: 0.7;
  border-color: #ff4d4f;
}

.card-header {
  background: #f8f9fa;
  padding: 16px 20px;
  border-bottom: 1px solid #e8e8e8;
}

.implementation-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.implementation-title h3 {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

.badges {
  display: flex;
  gap: 8px;
}

.badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
}

.badge.default {
  background: #52c41a;
  color: white;
}

.badge.status.available {
  background: #1890ff;
  color: white;
}

.badge.status.unavailable {
  background: #ff4d4f;
  color: white;
}

.card-body {
  padding: 20px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.label {
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.value {
  color: #1a1a1a;
  font-weight: 500;
}

.pay-products {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.product-tag {
  background: #f0f0f0;
  color: #666;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误提示 */
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.error-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.error-container h3 {
  color: #ff4d4f;
  margin-bottom: 8px;
}

.retry-button {
  background: #1890ff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  margin-top: 16px;
  transition: background 0.3s;
}

.retry-button:hover {
  background: #40a9ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .implementations-grid {
    grid-template-columns: 1fr;
  }

  .stats-section {
    grid-template-columns: 1fr;
  }
}
</style>